// BESIdeology

log using BESIdeology.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2019_W26_v26.0.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, J. Griffiths, S. Perrett. (2023) British Election Study Internet Panel Waves 1-26. DOI: 10.5255/UKDA-SN-8202-2// Accessed on March 11 2025.

// Social justice scale
* Clean "don't know" responses 
foreach var in cwTrans cwAuthors cwLanguage cwTraining {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguage cwTraining {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTrans cwAuthors rcwLanguage rcwTraining {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Other scales
* Clean "don't know" responses 
foreach var in leftRight likeLab {
    replace `var' = . if `var' == 9999
}

*Reverse scales so left/liberal coded high
foreach var in leftRight al_scale lr_scale {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

//Correlation
pwcorr SocJusValues rlr_scale rleftRight ral_scale likeLab [aweight=wt], sig 

// Ideological constraint
*Create graduate dummy variable
gen Graduate=.
replace Graduate=0 if inrange(p_edlevelUni, 0, 3)
replace Graduate=1 if inlist(p_edlevelUni, 4, 5)

*Create strong Labour support variable
gen strong_labour_support = (partyId == 2 & inlist(partyIdStrength, 1, 2))

// Correlations
pwcorr cwTrans cwAuthors rcwLanguage rcwTraining [aweight=wt], sig 
pwcorr cwTrans cwAuthors rcwLanguage rcwTraining if inrange(polAttention, 8, 10) [aweight=wt], sig 
pwcorr cwTrans cwAuthors rcwLanguage rcwTraining if Graduate==1 [aweight=wt], sig 
pwcorr cwTrans cwAuthors rcwLanguage rcwTraining if rleftRight>7 [aweight=wt], sig 
pwcorr cwTrans cwAuthors rcwLanguage rcwTraining if strong_labour_support==1 [aweight=wt], sig 

pwcorr cwTrans cwAuthors rcwLanguage rcwTraining if rleftRight<3 [aweight=wt], sig // Measure of constraint among right-wingers
pwcorr rlr_scale ral_scale if rleftRight<3 [aweight=wt], sig // Measure of constraint among right-wingers
pwcorr rlr_scale ral_scale if rleftRight>7 [aweight=wt], sig // Measure of constraint among left-wingers

log close
